package com.luo.contentprovider.domain.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 
 * @author lenovo
 *
 */
public class PersonDaoImpl implements PersonDao
{
	private static final String TAG = "dao";
	private static PersonDaoImpl instance;
	private DatabaseHelper helper;
	
	
	/**
	 * 
	 * @param ctx
	 */
	private PersonDaoImpl(Context ctx)
	{
		helper = new DatabaseHelper(ctx);
	}
	
	/**
	 * 
	 * @param ctx
	 */
	private PersonDaoImpl(DatabaseHelper thelper)
	{
		helper = thelper ;
	}
	
	public static synchronized PersonDao getInstance(Context ctx)
	{
		if (instance == null)
		{
			instance = new PersonDaoImpl(ctx);
		}
		return instance;
	}

	
	public static synchronized PersonDao getInstance(DatabaseHelper helper)
	{
		if (instance == null)
		{
			instance = new PersonDaoImpl(helper);
		}
		return instance;
	}

	@Override
	public void insertPerson(ContentValues va)
	{
		SQLiteDatabase  db = helper.getWritableDatabase();
		
		/////
		try
		{
			Log.i(TAG, "---insertPerson");
			db.insert(PersonSQLTable.TABLE_NAME, null, va);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	@Override
	public Cursor getAllPerson()
	{
		SQLiteDatabase  db = helper.getReadableDatabase();
		Cursor cur = null ;
		
		try
		{
			String[] columns = {PersonSQLTable.COLUMN_ID ,PersonSQLTable.COLUMN_NAME};
			
			String where = PersonSQLTable.COLUMN_ID+" > ?";
			String[] args = {"0"};
			
			 cur = db.query(PersonSQLTable.TABLE_NAME , columns , where,  args , null,null, null);
			 return cur ;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null ;
			
		}finally{
			
		}
	}
	
	
	
	
}




